package com.cw.reduce.filter;

import java.util.Vector;

public class RelativeFilter implements IFilter {
	private int type;

	public RelativeFilter(int type) {
		this.type = type;
	}

	private boolean compare(String operate, int x, int y) {
		if (x - y > 0 && !operate.equals("<") && !operate.equals("<=")
				&& !operate.equals("=")) {
			return true;
		}
		if (x - y < 0 && !operate.equals(">") && !operate.equals(">=")
				&& !operate.equals("=")) {
			return true;
		}
		if (x - y == 0 && !operate.equals(">") && !operate.equals("<")
				&& !operate.equals("<>")) {
			return true;
		}
		return false;
	}

	/**
	 * startFilter
	 * 
	 * @param condition
	 *            Vector
	 * @param target
	 *            Vector
	 * @return Vector
	 * @todo Implement this cp.gui.reduce.filter.IFilter method
	 */
	public Vector startFilter(Vector condition, Vector<Integer> target) {
		Vector result = new Vector();
		String condi = (String) condition.elementAt(0);

		switch (type) {
		case 0:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int a = value / 100;
				int b = 9 + value / 10 % 10 - value / 100;
				if (compare(condi, a, b)) {
					result.add(value);
				}
			}
			break;
		case 1:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int a = value / 100;
				int c = 9 + value % 10 - value / 10 % 10;
				if (compare(condi, a, c)) {
					result.add(value);
				}
			}
			break;
		case 2:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int a = value / 100;
				int d = 9 - value % 10;
				if (compare(condi, a, d)) {
					result.add(value);
				}
			}
			break;
		case 3:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int b = 9 + value / 10 % 10 - value / 100;
				int c = 9 + value % 10 - value / 10 % 10;
				if (compare(condi, b, c)) {
					result.add(value);
				}
			}
			break;
		case 4:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int b = 9 + value / 10 % 10 - value / 100;
				int d = 9 - value % 10;
				if (compare(condi, b, d)) {
					result.add(value);
				}
			}
			break;
		case 5:
			for (int i = 0; i < target.size(); i++) {
				int value = target.elementAt(i);
				int c = 9 + value % 10 - value / 10 % 10;
				int d = 9 - value % 10;
				if (compare(condi, c, d)) {
					result.add(value);
				}
			}
			break;

		}
		return result;
	}
}
